import axios from 'axios';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';

import WebGLVectorLayer from 'ol/layer/WebGLVector.js';

import { baseUrl } from './param';

async function loadWFSLayer(map) {
    try {
        const wfsUrl = baseUrl + '/ows';
        // 构造WFS请求参数
        const params = {
            service: 'WFS',
            version: '1.0.0',
            request: 'GetFeature',
            typeName: 'cesium:china',
            outputFormat: 'application/json',
            // srsName: 'EPSG:3857',
        };
        // 使用axios获取数据
        const response = await axios.get(wfsUrl, {
            params: params,
            headers: {
              'Accept': 'application/json'
            }
        });
        // 创建GeoJSON格式读取器
        const geojsonFormat = new GeoJSON();

        // 创建图层
        // const wfsLayer = new VectorLayer({
        const wfsLayer =new VectorLayer({
            properties: {
                name: 'wfsLayer'
            },
            source: new VectorSource({
                features: geojsonFormat.readFeatures(response.data, {
                    // defaultDataProjection: 'EPSG:4326',
                    // featureProjection: 'EPSG:3857'
                }),
            })
        })
        map.addLayer(wfsLayer);
        console.log('wfsLayer');
        return wfsLayer;
    } catch(err) {
        console.error('Error loading WFS layer:', err);
    }
}

export default loadWFSLayer;